/*
 *  $Id: DarkPower.cs 1051 2010-05-18 22:10:41Z thenn.erannor $
 *	This file is a part of Ragima CCG.
 *	(C) 2008 Ragima development group.
 *
 *	Licensed under the terms of the GNU GPL License version 2.
 *
 */

using System;
using System.Collections.Generic;

namespace Ragima.Cards001.Darkness.Spells {
    public class DarkPower: SpellCard<Creature> {
        public DarkPower(): base("001_d_s_dark_power") {
            Level = CompleteLevel.ReadyForTest;
            Element = ElementType.Darkness;
            Value = 100;
            Target = CreatureTarget.Own;
            AddManaCost(ElementType.Darkness, 2);
        }

        protected override void DoAction(TargetArgs<Creature> args) {
            DarkPowerCounter power = new DarkPowerCounter(args.Player);
            power.AddManaTo(args.Player);
            args.Target.InstantDeath();
            args.Player.Game.EndPhase();
        }

        private class DarkPowerCounter {
            public ElementValues Power;

            public DarkPowerCounter(Player owner) {
                Power = new ElementValues();
                DoCountPlayer(owner);
                DoCountPlayer(owner.Enemy);
            }

            public void AddManaTo(Player player) {
                foreach (ElementType element in ElementType.AllElements)
                    if (Power[element] > 0)
                        player.IncrementMana(element, Power[element]);
            }

            private void DoCountPlayer(Player owner) {
                DoCount(owner);
                foreach (SummonedObject summon in owner.GetSummonedObjects())
                    DoCount(summon);
            }

            private void DoCount(AbstractObject owner) {
                List<Affect> affects = new List<Affect>(owner.GetAffects(false, delegate(Affect affect) {
                    return affect.Dispellable && affect.Element == ElementType.Darkness && affect is SpellAffect;
                }));
                foreach (Affect affect in affects) {
                    (affect as SpellAffect).BaseCard.AddSumMana(Power);
                    affect.Dispell();
                }
            }
        }
    }
}